<?php
/**
 * 点将台
 * Enter description here ...
 * @author Administrator
 *
 */
class Module_overlordlnn{
	/**
	 * 构造函数
	 */
	function __construct() {
	
	}
	function index(){
		return $this->lnnrand();
	}
	/**
	 * 点将台初始化
	 * Enter description here ...
	 */
	function lnnrand(){
		$smarty = loadSmarty();
		$uin = sessionInfo('uin');
		if (empty($uin)){
			return View::error('获取君主ID失败.');
		}
		//点将台
		$cache_key = 'cache_'.$uin;
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		/**********************查询点将台***********************/
		$list = App_Overlord_Query::oneCollocate('app_overlord_generals_cache', $cache_key);
		if (empty($list)){
			$result = App_Overlord_Query::allCollocate('app_overlord_generals');
	
			$user_info = App_Overlord_Query::oneUser($uin);
			if (empty($user_info)){
				return View::error('获取君主数据失败.');
			}
			if (empty($result)){
				return View::error('数据库武将资料是空的,请联系管理员.');
			}
			//找出名将
			foreach ($result as $k => $row){
				$row['Value']['id'] = $k;
				//名将
				if ($row['Value']['state'] == 1){
					$generals_state[] = $row;
				}else{
					//普通武将
					$generals_common[] = $row;
				}
			}
		
			$odds = rand(0 , 99);
			//产生几率
			if($odds >= 5) {
			    //没有名将
			    $common_num = 3;
			    $state_num = 0;
			} else {
				// 名将数量
			    $state_num = 1;
			    $odds_s = rand(0 , 99); //再次几率
			    if($odds == 0 ){
			    	if ($odds_s <= 4){
			    		$state_num = 3;  // 4%的几率，三个名将
			    	}
			    }elseif($odds <= 1){ 
			    	if ($odds_s <= 10){
			    		$state_num = 2;   // 4%的几率，2个名将
			    	}
			    }
			    $common_num = 3 - $state_num;
			}
	
			// 根据 $common_num 和 $state_num 来获取将领
			if (!empty($common_num)){
				$common_count = count($generals_common) -1;
				//取普通将
				for ($i = 0; $i < $common_num; $i++){
					$common_rand = rand(0, $common_count);
					$key_rand['generals'][] = $result[$generals_common[$common_rand]['Value']['id']]['Key']; 
				}
			}
	
			if (!empty($state_num)){
				$state_count = count($generals_state) -1;
				//取名将
				for ($i = 0; $i < $state_num; $i++){
					$state_rand = rand(0, $state_count);
					$key_rand['generals'][] = $result[$generals_state[$state_rand]['Value']['id']]['Key']; 
				}
			}
			//下次刷新时间
			$strtotime = App_Overlord_GameStart::getStrtotime();
			
			$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
			$key_rand['time'] = $strtotime; 
			$key_rand['to_time'] = $time + 5 * 60; 

			$code = App_Overlord_Query::addCollocate('app_overlord_generals_cache', $cache_key, $key_rand);
			if ($code == 0){
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordlnn&a=lnn');
			}
		}else{
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordlnn&a=lnn');
		}
	}
	/**
	 * 武将招募页面
	 * Enter description here ...
	 */
	function lnn(){
		$smarty = loadSmarty();
		$uin = sessionInfo('uin');
		if (empty($uin)){
			return View::error('获取君主ID失败.');
		}
		$cache_key = 'cache_'.$uin;
		loadLib('App_Overlord_GameStart');   
		loadLib('App_Overlord_Query');   
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
		//查询点将台武将
		$result = App_Overlord_Query::oneCollocate('app_overlord_generals_cache', $cache_key);
		//查询君主资料
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info)){
			return View::error('获取君主数据失败.');
		}
		if (empty($result)){
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordlnn&a=lnnrand');
			break;
		}else{
			if (!empty($result['Value'])){
				if (!empty($result['Value']['generals'])){
					foreach ($result['Value']['generals'] as $k => $row){
						$list_key[] = $row;
					}
					//批量查询武将表
					$list_array = App_Overlord_Query::getInOverlord('app_overlord_generals', $list_key);
					foreach ($result['Value']['generals'] as $k => $row){
						foreach ($list_array as $k => $rs){
							if ($row == $rs['Key']){
								$list[] = $rs;
							}
						}
					}
					$smarty->assign('list' , $list);
				}
		
				//下次刷新时间
				$strtotime = App_Overlord_GameStart::getStrtotime();
		
				/************************CD完刷新点将台***********************/
				if($time >= $result['Value']['time']){
					
					$generals = App_Overlord_Query::allCollocate('app_overlord_generals');
					
					//找出名将
					foreach ($generals as $k => $row){
						$row['Value']['id'] = $k;
						//名将
						if ($row['Value']['state'] == 1){
							$generals_state[] = $row;
						}else{
							//普通武将
							$generals_common[] = $row;
						}
					}
					$odds = rand(0 , 99);
					//产生几率
					if($odds >= 5) {
					    //没有名将
					    $common_num = 3;
					    $state_num = 0;
					} else {
						// 名将数量
					    $state_num = 1;
					    $odds_s = rand(0 , 99); //再次几率
					    if($odds == 0 ){
					    	if ($odds_s <= 4){
					    		$state_num = 3;  // 4%的几率，三个名将
					    	}
					    }elseif($odds <= 1){ 
					    	if ($odds_s <= 10){
					    		$state_num = 2;   // 4%的几率，2个名将
					    	}
					    }
					    $common_num = 3 - $state_num;
					}
			
					// 根据 $common_num 和 $state_num 来获取将领
					if (!empty($common_num)){
						$common_count = count($generals_common) -1;
						//取普通将
						for ($i = 0; $i < $common_num; $i++){
							$common_rand = rand(0, $common_count);
							$key_rand['generals'][] = $generals[$generals_common[$common_rand]['Value']['id']]['Key']; 
						}
					}
					if (!empty($state_num)){
						$state_count = count($generals_state) -1;
						//取名将
						for ($i = 0; $i < $state_num; $i++){
							$state_rand = rand(0, $state_count);
							$key_rand['generals'][] = $generals[$generals_state[$state_rand]['Value']['id']]['Key']; 
						}
					}
					
					$key_rand['time'] = $strtotime; 
					$key_rand['to_time'] = 5 * 60; 
					$code = App_Overlord_Query::alterCollocate('app_overlord_generals_cache', $cache_key, $key_rand);
					if ($code == 0){
						App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordlnn&a=lnn');
					}
				}
			}
			$key = 'user_'.$uin;
			$generals_key = 'generals_'.$uin;
			$lv_generals = App_Overlord_GameStart::getGeneralsLv($user_info['Value']['lv']);
			$time = App_Overlord_GameStart::getEndDate($strtotime);
			$uin_generals = App_Overlord_Query::oneUin_Generals($uin);
			$count_generals = 0;
			if (!empty($uin_generals)){
				$count_generals = count($uin_generals['Value']);
			}
	
			$is_count = $lv_generals - $count_generals;
			
			$smarty->assign('gold',$user_info['Value']['gold']);
			$smarty->assign('count',$is_count);
			$smarty->assign('time',$time);
			return $smarty->fetch('app/Overlord/lnn.tpl.php');
		}
	}
	/**
	 * 招募武将
	 */
	function endlnn(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
		$g_id = isset($_POST['g_id']) ? trim($_POST['g_id']) : '';
		$uin = sessionInfo('uin');
		if (empty($uin)){
			return View::error('获取君主ID失败.');
		}
		$user_key = 'user_'.$uin;
		$cache_key = 'cache_'.$uin;
		$generals_key = 'generals_'.$uin;
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$result = App_Overlord_Query::oneCollocate('app_overlord_generals_cache', $cache_key);
		if (empty($result['Value'])){
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordlnn&a=lnn');
		}else{
			if (empty($result['Value']['generals'][$id])){
				App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordlnn&a=lnn');
			}
			if ($result['Value']['generals'][$id] == $g_id){
				
				$generals = App_Overlord_Query::oneCollocate('app_overlord_generals', $g_id);
				if (empty($generals)){
					return View::error('武将信息不存在.');
				}
				$list = App_Overlord_Query::oneUser($uin);
				if (empty($list)){
					return View::error('君主ID不存在.');
				}
				if (empty($list['Value']['task'])){
					App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordguide&a=guide');
					break;
				}
				//等级可带领的武将数目
				$lv_generals = App_Overlord_GameStart::getGeneralsLv($list['Value']['lv']);
				/************************扣钱操作********************/
				$list['Value']['gold'] -= $generals['Value']['gold'];
				if ($list['Value']['gold'] < 0){
					return View::error('你的黄金不足,不能招募将领');
				}
				$value = $generals['Value'];
				$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
				
				$num = rand(3, 4);
				
				//查询个人武将
				$uin_generals = App_Overlord_Query::oneUin_Generals($uin);
				$count_generals = 0;
				if (!empty($uin_generals)){
					//计算武将数量
					$count_generals = count($uin_generals['Value']);
				}
				if ($count_generals >= $lv_generals){
					return View::error('君上，你当前等级不能招募更多将领，等级越高招募将领越多，速去提升等级！<br />任务和攻城是提升等级的最佳途径。<br />', 0 ,array(
						array('name' => '去做任务' , 'url' => '/app.php?m=overlordtask&amp;a=opentask&amp;sid=' . sessionInfo('skey')),
						array('name' => '去攻城' , 'url' => '/app.php?m=overlordcity&amp;a=battlecity&amp;sid=' . sessionInfo('skey')),
						array('name' => '返回游戏主页' , 'url' => '/app.php?m=overlorduser&amp;sid=' . sessionInfo('skey')),
						));
				}
				
				foreach ($result['Value']['generals'] as $k => $row){
					if ($k == $id && $row == $g_id){
					}else{
						$cache_value['generals'][] = $row;	
					}
				}
		
				$cache_value['time'] = $result['Value']['time'];
				$cache_value['to_time'] = $result['Value']['to_time'];
				
				//初始化武将带兵数
				$make_count = 500;
				//初始攻击
				$grow_ap = $value['ap'];
				//初始防御力
				$grow_defense = $value['defense'];
				//初始生命力
				$grow_hp = $value['hp'];
				loadLib('App_Overlord_Collocate');
				//获取技能列表
				$skills = App_Overlord_Collocate::getSkill();
				//看下招募过来的武将是否有加兵技能
				if (!empty($value['skills']['strengthen'])){
					foreach ($value['skills']['strengthen'] as $k => $row){
						if ($k == 'staff'){
							$skills_num = $skills['strengthen'][$k][$row[$k]]['num'];
							$make_count += $make_count * $skills_num/100;
							break;
						}
					}
				}
				//武技
				if (!empty($row['skills']['study'])){
					foreach ($row['skills']['study'] as $k => $skills_row){
						if ($k == 'ap'){
							//攻击力加成
							$skills_num = $skills['study'][$k][$skills_row]['num'];
							$grow_ap += $grow_ap * $skills_num/100;
						}elseif ($k == 'defense'){
							//防御力加成
							$skills_num = $skills['study'][$k][$skills_row]['num'];
							$grow_defense += $grow_defense * $skills_num/100;
						}elseif ($k == 'hp'){
							//防御力加成
							$skills_num = $skills['study'][$k][$skills_row]['num'];
							$grow_hp += $grow_hp * $skills_num/100;
						}
					}
				}
				//个人武将表
				if (empty($uin_generals)){
					/************************写入武将表**********************/
					$is_value[$time] = array('name' => $generals['Value']['name'], 'generals' => $g_id , 'lv' => 1 , 'exp' => 0 , 'ap' => $grow_ap , 'grow_ap' => $value['grow_ap'] , 'defense' => $grow_defense , 'grow_defense' => $value['grow_defense'] , 'hp' => $grow_hp , 'grow_hp' => $value['grow_hp'] , 'make' => array() , 'make_count' => $make_count , 'physical' => 10 , 'equipment' => array() , 'skills' => $value['skills'] , 'num' => $num , 'state' =>0 , 'to_uin' => 0 , 'time' => $time , 'strategy' => 3 , 'generals_state' => $value['state']);
					
					$code = App_Overlord_Query::addUin_Generals($uin , $is_value);
				}else{
					$uin_generals['Value'][$time] = array('name' => $generals['Value']['name'], 'generals' => $g_id , 'lv' => 1 , 'exp' => 0 , 'ap' => $grow_ap , 'grow_ap' => $value['grow_ap'] , 'defense' => $grow_defense , 'grow_defense' => $value['grow_defense'] , 'hp' => $grow_hp , 'grow_hp' => $value['grow_hp'] , 'make' => array() , 'make_count' => $make_count , 'physical' => 10 , 'equipment' => array() , 'skills' => $value['skills'] , 'num' => $num , 'state' =>0 , 'to_uin' => 0 , 'time' => $time , 'strategy' => 3 , 'generals_state' => $value['state']);
					$code = App_Overlord_Query::alterUin_Generals($uin, $uin_generals['Value']);
				}
				//消费记录
				$information['name'] = '招募将领【'.$generals['Value']['name'].'】花费了'.$generals['Value']['gold'] .'黄金';
				$information['time'] = $time;
				//消费记录
				App_Overlord_GameStart::getConsume($information);
				if ($code == 0){
					loadLib('App_Overlord_Task');
					//判断任务
					$task_type = App_Overlord_Task::getTaskType();
					//完成招募武将任务
					App_Overlord_Task::getGo('main' , $task_type['main']['endlnn']);
					if (!empty($value['state'])){
						//名将加5点经验
						$list['Value']['exp'] += 5;
					}else{
						//普通将加2点经验
						$list['Value']['exp'] += 2;
					}
					//人物表
					$code = App_Overlord_Query::alterUser($uin, $list['Value']);
					//修改点将台武将
					$code = App_Overlord_Query::alterCollocate('app_overlord_generals_cache', $cache_key, $cache_value);
					//名将就提示
					
					App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordlnn&a=general_pege&id='.$g_id.'&time='.$time);
					
				}
			}else{
				return View::error('此武将已被别人招走.');
			}
		}
	}
	/**
	 * 招募对方将领
	 */
	function to_lnn(){
		$to_uin = isset($_GET['to_uin']) ? intval($_GET['to_uin']) : null;
		$uin = SessionInfo('uin');
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
		if ($to_uin == $uin) {
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordlnn&a=lnn');
			break;
		}
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		//查询好友
		loadLib('Friend');
		$smarty = loadSmarty();
		//查询对方城池信息
		$to_city_info = App_Overlord_Query::oneCity($to_uin);
		if (empty($to_city_info)){
			return View::error('获取君主信息失败!');
		}
		//查询对方信息
		$list_info = App_Overlord_Query::oneUser($to_uin);
		if (empty($list_info)){
			return View::error('获取君主信息失败!');
		}
		//查询好友列表
		$friend_list = Friend::get($uin);
		$in = in_array($to_uin,$friend_list);
		if (empty($in) && $to_city_info['Value']['hkee']['to_uin'] != $uin){
			return View::error('对方不是你的好友!');
		}
		if (!empty($to_city_info['Value']['hkee']['to_uin']) && $to_city_info['Value']['hkee']['to_uin'] != $uin && $to_city_info['Value']['hkee']['to_uin'] != $to_uin){
			return View::error('你的好友被别人占领，赶快返回解救吧!');
		}
		//点将台
		$cache_key = 'cache_'.$to_uin;
		/**********************查询点将台***********************/
		$cache_info = App_Overlord_Query::oneCollocate('app_overlord_generals_cache', $cache_key);
		if (empty($cache_info)){
			$generals_info = App_Overlord_Query::allCollocate('app_overlord_generals');
			if (empty($generals_info)){
				return View::error('数据库武将资料是空的,请联系管理员.');
			}
			//找出名将
			foreach ($generals_info as $k => $row){
				$row['Value']['id'] = $k;
				//名将
				if ($row['Value']['state'] == 1){
					$generals_state[] = $row;
				}else{
					//普通武将
					$generals_common[] = $row;
				}
			}
		
			$odds = rand(0 , 99);
			//产生几率
			if($odds >= 5) {
			    //没有名将
			    $common_num = 3;
			    $state_num = 0;
			} else {
			   // 名将数量
			    $state_num = 1;
			    $odds_s = rand(0 , 99); //再次几率
			    if($odds == 0 ){
			    	if ($odds_s <=4){
			    		$state_num = 3;  // 4%的几率，三个名将
			    	}
			    }elseif($odds <= 1){ 
			    	if ($odds_s <=4){
			    		$state_num = 2;   // 4%的几率，2个名将
			    	}
			    }
			    $common_num = 3 - $state_num;
			}
	
			// 根据 $common_num 和 $state_num 来获取将领
			if (!empty($common_num)){
				$common_count = count($generals_common) -1;
				//取普通将
				for ($i = 0; $i < $common_num; $i++){
					$common_rand = rand(0, $common_count);
					$key_rand['generals'][] = $generals_info[$generals_common[$common_rand]['Value']['id']]['Key']; 
				}
			}
	
			if (!empty($state_num)){
				$state_count = count($generals_state) -1;
				//取名将
				for ($i = 0; $i < $state_num; $i++){
					$state_rand = rand(0, $state_count);
					$key_rand['generals'][] = $generals_info[$generals_state[$state_rand]['Value']['id']]['Key']; 
				}
			}
			//下次刷新时间
			$strtotime = App_Overlord_GameStart::getStrtotime();
			
			$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
			$key_rand['time'] = $strtotime; 
			$key_rand['to_time'] = $time + 5 * 60; 

			$code = App_Overlord_Query::addCollocate('app_overlord_generals_cache', $cache_key, $key_rand);
			if ($code == 0){
				App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordlnn&a=to_lnn&to_uin='.$to_uin);
			}
		}
		
		if (!empty($cache_info['Value'])){
			if (!empty($cache_info['Value']['generals'])){
				foreach ($cache_info['Value']['generals'] as $k => $row){
					$list_key[] = $row;
				}
				//批量查询武将表
				$list_array = App_Overlord_Query::getInOverlord('app_overlord_generals', $list_key);
				foreach ($cache_info['Value']['generals'] as $k => $row){
					foreach ($list_array as $k => $rs){
						if ($row == $rs['Key']){
							$list[] = $rs;
						}
					}
				}
	
				$smarty->assign('list' , $list);
			}
			//下次刷新时间
			$strtotime = App_Overlord_GameStart::getStrtotime();
	
			/************************CD完刷新点将台***********************/
			if($time >= $cache_info['Value']['time']){
				$generals = App_Overlord_Query::allCollocate('app_overlord_generals');
				
				//找出名将
				foreach ($generals as $k => $row){
					$row['Value']['id'] = $k;
					//名将
					if ($row['Value']['state'] == 1){
						$generals_state[] = $row;
					}else{
						//普通武将
						$generals_common[] = $row;
					}
				}
				$odds = rand(0 , 99);
				//产生几率
				if($odds >= 5) {
				    //没有名将
				    $common_num = 3;
				    $state_num = 0;
				} else {
					// 名将数量
				    $state_num = 1;
				    $odds_s = rand(0 , 99); //再次几率
				    if($odds == 0 ){
				    	if ($odds_s <= 4){
				    		$state_num = 3;  // 4%的几率，三个名将
				    	}
				    }elseif($odds <= 1){ 
				    	if ($odds_s <= 10){
				    		$state_num = 2;   // 4%的几率，2个名将
				    	}
				    }
				    $common_num = 3 - $state_num;
				}
		
				// 根据 $common_num 和 $state_num 来获取将领
				if (!empty($common_num)){
					$common_count = count($generals_common) -1;
					//取普通将
					for ($i = 0; $i < $common_num; $i++){
						$common_rand = rand(0, $common_count);
						$key_rand['generals'][] = $generals[$generals_common[$common_rand]['Value']['id']]['Key']; 
					}
				}
				if (!empty($state_num)){
					$state_count = count($generals_state) -1;
					//取高级
					for ($i = 0; $i < $state_num; $i++){
						$state_rand = rand(0, $state_count);
						$key_rand['generals'][] = $generals[$generals_state[$state_rand]['Value']['id']]['Key']; 
					}
				}
				
				$key_rand['time'] = $strtotime; 
				$key_rand['to_time'] = 5 * 60; 
				$code = App_Overlord_Query::alterCollocate('app_overlord_generals_cache', $cache_key, $key_rand);
				if ($code == 0){
					App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordlnn&a=to_lnn&to_uin='.$to_uin);
				}
			}
		}
		
		//查询君主信息
		$user_info = App_Overlord_Query::oneUser($uin);
		$lv_generals = App_Overlord_GameStart::getGeneralsLv($user_info['Value']['lv']);
		$uin_generals = App_Overlord_Query::oneUin_Generals($uin);
		$count_generals = 0;
		if (!empty($uin_generals)){
			$count_generals = count($uin_generals['Value']);
		}
		$is_count = $lv_generals - $count_generals;
		$endtime = App_Overlord_GameStart::getEndDate($strtotime);
		$smarty->assign('gold',$user_info['Value']['gold']);	
		$smarty->assign('count',$is_count);
		$smarty->assign('time',$endtime);
		$smarty->assign('list_info',$list_info['Value']);
		$smarty->assign('to_uin',$to_uin);
		return $smarty->fetch('app/Overlord/to_lnn.tpl.php');
	}
	/**
	 * 招募好友将领
	 */
	function to_endlnn(){
		$to_uin = isset($_POST['to_uin']) ? intval($_POST['to_uin']) : null;
		$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
		$g_id = isset($_POST['g_id']) ? trim($_POST['g_id']) : '';
		$uin = SessionInfo('uin');
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
		if ($to_uin == $uin) {
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordlnn&a=lnn');
			break;
		}
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		//查询好友
		loadLib('Friend');
		$smarty = loadSmarty();
		//查询对方城池信息
		$to_city_info = App_Overlord_Query::oneCity($to_uin);
		if (empty($to_city_info)){
			return View::error('获取君主信息失败!');
		}
		//查询对方信息
		$list_info = App_Overlord_Query::oneUser($to_uin);
		if (empty($list_info)){
			return View::error('获取君主信息失败!');
		}
		//查询好友列表
		$friend_list = Friend::get($uin);
		$in = in_array($to_uin,$friend_list);
		if (empty($in) && $to_city_info['Value']['hkee']['to_uin'] != $uin){
			return View::error('对方不是你的好友!');
		}
		if (!empty($to_city_info['Value']['hkee']['to_uin']) && $to_city_info['Value']['hkee']['to_uin'] != $uin && $to_city_info['Value']['hkee']['to_uin'] != $to_uin){
			return View::error('你的好友被别人占领，赶快返回解救吧!');
		}
		$cache_info = App_Overlord_Query::oneCollocate('app_overlord_generals_cache' , 'cache_' .$to_uin);
		if (empty($cache_info['Value'])){
			return View::error('数据异常!');
		}else{
			
			if ($cache_info['Value']['generals'][$id] == $g_id){
				
				$generals = App_Overlord_Query::oneCollocate('app_overlord_generals', $g_id);
				if (empty($generals)){
					return View::error('武将信息不存在.');
				}
				$list = App_Overlord_Query::oneUser($uin);
				if (empty($list)){
					return View::error('君主ID不存在.');
				}
				if (empty($list['Value']['task'])){
					return View::error('数据异常!');
				}
				$cache_time = $cache_info['Value']['time'] - 6 * 3600 + 5 * 60;
				if ($cache_time > $time){
					return View::error($list_info['Value']['name'].'的武将'.$generals['Value']['name'].'正在保护期,不能被招募!');
				}
				//等级可带领的武将数目
				$lv_generals = App_Overlord_GameStart::getGeneralsLv($list['Value']['lv']);
				/************************扣钱操作********************/
				$list['Value']['gold'] -= $generals['Value']['gold'];
				if ($list['Value']['gold'] < 0){
					return View::error('你的黄金不足,不能招募将领');
				}
				$value = $generals['Value'];
				
				$num = rand(3, 4);
				
				//查询个人武将
				$uin_generals = App_Overlord_Query::oneUin_Generals($uin);
				$count_generals = 0;
				if (!empty($uin_generals)){
					//计算武将数量
					$count_generals = count($uin_generals['Value']);
				}
				if ($count_generals >= $lv_generals){
					return View::error('君上，你当前等级不能招募更多将领，等级越高招募将领越多，速去提升等级！<br />任务和攻城是提升等级的最佳途径。<br />', 0 ,array(
						array('name' => '去做任务' , 'url' => '/app.php?m=overlordtask&amp;a=opentask&amp;sid=' . sessionInfo('skey')),
						array('name' => '去攻城' , 'url' => '/app.php?m=overlordcity&amp;a=battlecity&amp;sid=' . sessionInfo('skey')),
						array('name' => '返回游戏主页' , 'url' => '/app.php?m=overlorduser&amp;sid=' . sessionInfo('skey')),
						));
				}
				
				foreach ($cache_info['Value']['generals'] as $k => $row){
					if ($k == $id && $row == $g_id){
					}else{
						$cache_value['generals'][] = $row;	
					}
				}
		
				$cache_value['time'] = $cache_info['Value']['time'];
				$cache_value['to_time'] = $cache_info['Value']['to_time'];
				
				//初始化武将带兵数
				$make_count = 500;
				//初始攻击
				$grow_ap = $value['ap'];
				//初始防御力
				$grow_defense = $value['defense'];
				//初始生命力
				$grow_hp = $value['hp'];
				loadLib('App_Overlord_Collocate');
				//获取技能列表
				$skills = App_Overlord_Collocate::getSkill();
				//看下招募过来的武将是否有加兵技能
				if (!empty($value['skills']['strengthen'])){
					foreach ($value['skills']['strengthen'] as $k => $row){
						if ($k == 'staff'){
							$skills_num = $skills['strengthen'][$k][$row[$k]]['num'];
							$make_count += $make_count * $skills_num/100;
							break;
						}
					}
				}
				//武技
				if (!empty($row['skills']['study'])){
					foreach ($row['skills']['study'] as $k => $skills_row){
						if ($k == 'ap'){
							//攻击力加成
							$skills_num = $skills['study'][$k][$skills_row]['num'];
							$grow_ap += $grow_ap * $skills_num/100;
						}elseif ($k == 'defense'){
							//防御力加成
							$skills_num = $skills['study'][$k][$skills_row]['num'];
							$grow_defense += $grow_defense * $skills_num/100;
						}elseif ($k == 'hp'){
							//防御力加成
							$skills_num = $skills['study'][$k][$skills_row]['num'];
							$grow_hp += $grow_hp * $skills_num/100;
						}
					}
				}
				//个人武将表
				if (empty($uin_generals)){
					/************************写入武将表**********************/
					$is_value[$time] = array('name' => $generals['Value']['name'], 'generals' => $g_id , 'lv' => 1 , 'exp' => 0 , 'ap' => $grow_ap , 'grow_ap' => $value['grow_ap'] , 'defense' => $grow_defense , 'grow_defense' => $value['grow_defense'] , 'hp' => $grow_hp , 'grow_hp' => $value['grow_hp'] , 'make' => array() , 'make_count' => $make_count , 'physical' => 10 , 'equipment' => array() , 'skills' => $value['skills'] , 'num' => $num , 'state' =>0 , 'to_uin' => 0 , 'time' => $time , 'strategy' => 3 , 'generals_state' => $value['state']);
					
					$code = App_Overlord_Query::addUin_Generals($uin , $is_value);
				}else{
					$uin_generals['Value'][$time] = array('name' => $generals['Value']['name'], 'generals' => $g_id , 'lv' => 1 , 'exp' => 0 , 'ap' => $grow_ap , 'grow_ap' => $value['grow_ap'] , 'defense' => $grow_defense , 'grow_defense' => $value['grow_defense'] , 'hp' => $grow_hp , 'grow_hp' => $value['grow_hp'] , 'make' => array() , 'make_count' => $make_count , 'physical' => 10 , 'equipment' => array() , 'skills' => $value['skills'] , 'num' => $num , 'state' =>0 , 'to_uin' => 0 , 'time' => $time , 'strategy' => 3 , 'generals_state' => $value['state']);
					$code = App_Overlord_Query::alterUin_Generals($uin, $uin_generals['Value']);
				}
				
				if ($code == 0){
					if (!empty($value['state'])){
						//名将加5点经验
						$list['Value']['exp'] += 5;
					}else{
						//普通将加2点经验
						$list['Value']['exp'] += 2;
					}
					//人物表
					$code = App_Overlord_Query::alterUser($uin, $list['Value']);
					//修改点将台武将
					App_Overlord_Query::alterCollocate('app_overlord_generals_cache', 'cache_' .$to_uin, $cache_value);
					//名将就提示
					
					App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordlnn&a=general_pege&id='.$g_id.'&time='.$time .'&to_uin=' .$to_uin);
				}
			}else{
				return View::error('此武将已被别人招走.');
			}
		}
	}
	/**
	 * 招募将领提示
	 */
	function general_pege(){
		$id = isset($_GET['id']) ? trim($_GET['id']) : '';
		$time = isset($_GET['time']) ? trim($_GET['time']) : '';
		$to_uin = isset($_GET['to_uin']) ? trim($_GET['to_uin']) : '';
		$uin = SessionInfo('uin');
		$smarty = loadSmarty();
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$generals_info = App_Overlord_Query::oneUin_Generals($uin);
		if (empty($generals_info['Value'][$time])) {
			return View::error('没有该武将!');
		}
		if ($generals_info['Value'][$time]['generals'] != $id) {
			return View::error('数据异常!');
		}
		$smarty->assign('list',$generals_info['Value'][$time]);
		$smarty->assign('id',$time);
		$smarty->assign('to_uin',$to_uin);
		$smarty->assign('uin',$uin);
		$smarty->assign('path' , $id);
		return $smarty->fetch('app/Overlord/general_pege.tpl.php');
	}
}
# end of script